#ifndef __UTIL__
#define __UTIL__
#include <iostream>
#include <ctime>
#include <sys/stat.h>
#include <string>

namespace bitlog
{
    class DateUtil
    {
    public:
        static size_t now()
        {
            return (size_t)time(nullptr);
        }
    };

    class FileUtil
    {
    public:
        //判断文件是否存在
        static bool isExists(const std::string& fileName)
        {
            struct stat st;
            return stat(fileName.c_str(),&st) == 0;
        }
        static std::string getPath(const std::string& fileName)
        {
            if(fileName.empty())
            {
                return ".";
            }
            size_t pos = fileName.find_last_of("/\\");
            if(pos == std::string::npos)
            {
                return ".";
            }
            return fileName.substr(0,pos+1);
        }
        static void createDirectory(const std::string& path)
        {
            if(path.empty())
            {
                return;
            }
            if(isExists(path))
            {
                return;
            }
            size_t pos = 0,idx = 0;
            while(idx < path.size())
            {
                pos = path.find_first_of("/\\",idx);
                if(pos == std::string::npos)
                {
                    mkdir(path.c_str(),0755);
                    return;
                }
                if(pos == idx)
                {
                    idx = pos + 1;
                    continue;
                }
                std::string subdir = path.substr(0,pos);
                if(subdir == "." || subdir == "..")
                {
                    idx = pos+1;
                    continue;
                }
                if(isExists(subdir))
                {
                    idx = pos+1;
                    continue;
                }
                mkdir(subdir.c_str(),0755);
                idx = pos + 1;
            }
        }
    };
}
#endif